package com.teamsun.datasource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

/**
 * <p>获取数据库配置信息</p>
 */
public class DBConfigService {

    private JdbcTemplate jdbcTemplate;

    /**
     * 查询数据库配置信息
     * @param dbName  数据库名称
     * @return 数据库配置信息
     */
    public DBCfg getDBCfg(String dbName) throws Exception {
        String querySql = "select\n" +
                "          t.db_type as \"dbType\",\n" +
                "           t.db_name as \"dbName\",\n" +
                "           t.db_comment as \"dbCommment\",\n" +
                "           t.db_driver as \"driverClass\",\n" +
                "           t.db_username as \"userName\",\n" +
                "           t.db_password as \"passworld\",\n" +
                "           t.db_url as \"jdbcURL\"" +
                "          from TB_RPT_DBCFG t\n" +
                "          where t.db_name = '" + dbName + "'";

        RowMapper<DBCfg> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(DBCfg.class);
        DBCfg dbCfg = (DBCfg) jdbcTemplate.queryForObject(querySql, rowMapper);
        return dbCfg;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
